package com.itqn.servlet;

import com.alibaba.fastjson.JSONArray;
import com.itqn.pojo.Category;
import com.itqn.service.CategoryService;
import com.itqn.service.Impl.CategoryServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "/CategoryEchoServlet")
public class CategoryEchoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决中文乱码问题
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        //获取参数
        String id = request.getParameter("id");
        //创建业务逻辑层对象
        CategoryService categoryService=new CategoryServiceImpl();
        try {
            Category  category = categoryService.findCategoryById(Integer.parseInt(id));
            String categorystr = JSONArray.toJSONString(category);
            PrintWriter writer = response.getWriter();
            writer.write(categorystr);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}